<%@page import="java.util.TreeSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.TreeMap"%>
<%@page import="org.jsoft.common.GetRandomColor"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";

	String group = "MSLine";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
<SCRIPT LANGUAGE="Javascript"
	SRC="<%=path%>/FusionChart/Includes/js/FusionCharts.js"></SCRIPT>

<script>
	function showAlert() {
		var years = document.getElementsByName("showYears");
		var count = 0;
		for ( var i = 0; i < years.length; i++) {
			var aItem = years.item(i).checked;
			if (aItem == true) {
				count++;
			}
		}
		if (count < 2) {
			window.alert("您选择的年份数量不得小于2！");
		} else {
			document.forms[0].submit();
		}
	}
</script>
</head>
<body>
	<%
		//得到的要显示的年
		TreeSet<String> yearsSet = (TreeSet<String>) request
				.getAttribute("showYears");

		Object[] years = yearsSet.toArray();
		//得到的数据
		TreeMap<String, TreeMap<String, String>> monthsInYears = (TreeMap<String, TreeMap<String, String>>) request
				.getAttribute("monthsInYears");

		TreeSet<String> yearsToShow = new TreeSet<String>(
				monthsInYears.keySet());

		Object[] yearsFromServlet = yearsToShow.toArray();

		StringBuffer strBuf = new StringBuffer();
		strBuf.append("<graph caption='业绩年K线对比图' subcaption='' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='3' numVdivlines='0'  rotateNames='1'>");
		strBuf.append("<categories >");
		for (int i = 1; i <= 12; i++) {
			strBuf.append("<category name='" + i + "月' />");
		}
		strBuf.append("</categories>");

		for (int i = 0; i < yearsFromServlet.length; i++) {
			//对于每一年的操作
			TreeMap<String, String> mapOfYear = monthsInYears
					.get(yearsFromServlet[i]);
			String color = GetRandomColor.getRandomColorString(40, 240);
			strBuf.append("<dataset seriesName='"
					+ yearsFromServlet[i]
					+ "' color='"
					+ color
					+ "' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>");
			for (int j = 1; j < 12; j++) {
				String monthString = "" + j;
				if (monthString.length() < 2) {
					monthString = "0" + monthString;
				}
				//对于每一年的操作
				if (mapOfYear.keySet().contains(monthString)) {
					strBuf.append("<set value='"
							+ mapOfYear.get(monthString) + "' />");
				} else {
					strBuf.append("<set value='1327' />");
				}
			}
			strBuf.append("</dataset>");
		}
		strBuf.append("</graph>");
		
	%>
	<%
		/**
		此处必须引到FusionChart下的/Includes/swf/文件夹
		 */
		String graphType = path + "/FusionChart/Includes/swf/" + group
				+ ".swf";
		request.setCharacterEncoding("UTF-8");
	%>
	<center>
		<h2 id="showTitle">业绩年K线对比图</h2>
		<br />

		<div id="showGraph">
			<jsp:include
				page="../FusionChart/Includes/jsp/FusionChartsHTMLRenderer.jsp"
				flush="true">
				<jsp:param name="chartSWF" value="<%=graphType%>" />
				<jsp:param name="strURL" value="" />
				<jsp:param name="strXML" value="<%=strBuf.toString()%>" />
				<jsp:param name="chartId" value="accounts_code" />
				<jsp:param name="chartWidth" value="800" />
				<jsp:param name="chartHeight" value="450" />
				<jsp:param name="debugMode" value="false" />
			</jsp:include>
		</div>
		<div>
			<form action="<%=path%>/yearContrastServlet" method="post"
				name="showYearConstrast">
				<%
					for (int i = 0; i < years.length; i++) {
				%>
				<input type="checkbox" value="<%=years[i]%>" name="showYears" /><%=years[i]%>年&nbsp;
				<%
					}
				%>
				<input type="button" onclick="showAlert()" value="查看">
			</form>
		</div>
	</center>
</body>